// pages/PostsModule/generalPost.js
const app = getApp()
const icons = {
    temporary: "../../resource/icon/temporary.png",
    like0: "../../resource/icon/like0.png",
    like1: "../../resource/icon/like1.png",
    dislike0: "../../resource/icon/dislike0.png",
    dislike1: "../../resource/icon/dislike1.png",
    star0: "../../resource/icon/star0.png",
    star1: "../../resource/icon/star1.png",
}
Page({
    data: {
        statusBarHeight: app.globalData.statusBarHeight,//手机状态栏高度
        postId: null,//帖文id
        postContent: {
            postId: null,//帖文id
            barId: null,//帖文所属版面id
            postType: null,//帖文类型（0-普通，1-热门，2-加精，3-置顶）
            postTitle: null,//贴文标题
            postText: null,//贴文内容
            creatorId: null,//发帖人id
            creatorAvatarPath: null,//发帖人头像
            createTime: null,//发帖时间（yyyy-MM-dd HH:mm）
            popularity: null,//贴子热度
            viewSum: null,//该贴浏览次数
            likeSum: null,//该贴点赞次数
            collectSum: null,//该贴收藏次数
            dislikeSum: null,//该贴被踩次数
            hasLike: null,//是否已点赞（true-已点赞，false-未点赞）
            hasCollect: null,//是否已收藏（true-已收藏，false-未收藏）
            hasDislike: null//是否踩过该贴（true-踩过，false-未踩过）
        },
        creatorName: null,//发帖人昵称
        barName: null,//版面名
        comment: "",//已输入的评论
        inputValue: "",//已输入的评论（用于清空输入框）
        pageNum: 1,//评论下一页的页码
        numPerPage: 100,//每页的评论数
        commentList: {},
        /*
        数组元素属性：
        commentId,          //评论id
        postId,             //所属帖文id
        objectId,           //评论针对对象的id
        creatorId,          //评论人id
        commentContent,     //评论内容
        createTime,         //评论发布时间
        lastEditTime,       //最近一次修改时间
        likeSum,            //点赞数
        dislikeSum,         //点踩数
        hasLike,            //是否赞过
        hasDislike,         //是否踩过
        creatorAvatarPath,  //评论人头像
        creatorName         //评论人昵称
        objectContent       //--回复对象内容--（二次请求）
        */
        listEnd: false,//是否到达页底
        inputShowed: false,//输入框是否获得焦点
        commentTarget: null,//回复对象id，若无则为null，默认为null
        inputHolder: "发表评论",
        likeIcon: icons.like0,
        dislikeIcon: icons.dislike0,
        starIcon: icons.star0,

    },
    onLoad: function (options) {
        this.setData({
            postId: options.postid
        })
        this.initPostInfo()
        this.initComment()
    },

    initPostInfo(){
        var that = this
        //取得帖头信息（帖文、版面、发帖人等）
        wx.request({
            url: `http://${app.globalData.target}//post/getPost`,
            data: {
                postId: that.data.postId
            },
            header: {
                "hello-token": wx.getStorageSync("hello-token")
            },
            method: 'GET',
            success: function(result){
                console.log("getPost result:")
                console.log(result)
                that.setData({
                    postContent: result.data.result
                })
                //获取图标状态
                that.initButtomIcon()
                //取得creator名
                that.getNickName()
                //取得版面名
                that.getBarName()
            },
            fail: function(e){
                console.log(e)
            }
        })
    },
    initButtomIcon(){
        var that = this
        if(that.data.postContent.hasLike == true){
            that.setData({
                likeIcon: icons.like1
            })
        }
        if(that.data.postContent.hasDislike == true){
            that.setData({
                dislikeIcon: icons.dislike1
            })
        }
        if(that.data.postContent.hasCollect == true){
            that.setData({
                starIcon: icons.star1
            })
        }
    },
    getNickName(){
        var that = this
        wx.request({
            url: `http://${app.globalData.target}/user/displayedInfo`,
            data: {
                userId: that.data.postContent.creatorId
            },
            header: {
                "hello-token": wx.getStorageSync("hello-token")
            },
            method: 'GET',
            success: function(result){
                console.log(result)
                that.setData({
                    creatorName: result.data.result.userName
                })
            },
            fail: function(e){
                console.log(e)
            }
        })
    },
    getBarName(){
        var that = this
        console.log(that.data.postContent.barId)
        wx.request({
            url: `http://${app.globalData.target}/bar/getBarConfig`,
            data: {
                barId: that.data.postContent.barId
            },
            header: {
                "hello-token": wx.getStorageSync("hello-token")
            },
            method: 'GET',
            success: function(result){
                console.log(result)
                that.setData({
                    barName: result.data.result.barName
                })
                wx.setNavigationBarTitle({
                    title: that.data.barName,
                })
            },
            fail: function(e){
                console.log(e)
            }
        })
    },
    initComment(){
        var that = this
        //取得评论列表
        wx.request({
            url: `http://${app.globalData.target}/comment/getList`,
            data: {
                postId: that.data.postId,
                currentPage: 1,
                pageSize: that.data.numPerPage,
                sortType: 1//默认按时间排序
            },
            header: {
                "hello-token": wx.getStorageSync("hello-token")
            },
            method: 'GET',
            success: function(result){
                console.log("getComment result:")
                console.log(result)
                that.setData({
                    commentList: result.data.result.commentList
                })
                //二次请求得到评论引用
                that.getReference()
                console.log(that.data.commentList)
                //判断是否到达最后一页
            },
            fail: function(e){
                console.log(e)
            }
        })
    },
    getReference(){
        var that = this
        var commentList = that.data.commentList
        for(var i=0; i<commentList.length; i++){
            that.getSpecifiedComment(commentList[i].objectId, i)
        }
    },
    getSpecifiedComment(commentId, i){
        var that = this
        if(commentId != null){
            wx.request({
                url: `http://${app.globalData.target}/comment/get`,
                data: {
                    commentId: commentId
                },
                header: {
                    "hello-token": wx.getStorageSync("hello-token")
                },
                method: 'GET',
                success: function(result){
                    that.setData({
                        [`commentList[${i}].objectContent`]: result.data.result.commentContent
                    })
                },
                fail: function(e){
                    console.log(e)
                }
            })
        }
        
    },

    goToBar: function(){
        var barid = this.data.postContent.barId
        wx.navigateTo({
            url: `../TopicsModule/singleTopic?barid=${barid}`
        })
    },
    bindInputChange: function(e){
        var commentTarget = this.data.commentTarget
        this.setData({
            comment: e.detail.value
        })
    },
    commit: function(e){
        var that = this
        //发布评论
        var comment = that.data.comment
        var target
        if(that.data.commentTarget == null){
            target = null
        }else{
            target = that.data.commentTarget
        }
        if(comment == ""){
            wx.showToast({
                title: "评论内容不可为空",
                icon: 'none',
                duration: 1000
            })
        }else if(comment.length < 5){
            wx.showToast({
                title: "评论长度过短",
                icon: 'none',
                duration: 1000
            })
        }else{
            wx.request({
                url: `http://${app.globalData.target}/comment/add`,
                data: {
                    postId: that.data.postId,
                    objectId: target,
                    commentContent: comment
                },
                header: {
                    "hello-token": wx.getStorageSync("hello-token")
                },
                method: 'POST',
                success: function(result){
                    console.log("commit result:")
                    console.log(result)
                    that.setData({
                        inputValue: ""
                    })
                    wx.showToast({
                        title: "发送成功",
                        icon: "success",
                        duration: 1000
                    })
                    //刷新评论列表
                    that.initComment()
                },
                fail: function(e){
                    console.log(e)
                }
            })
        }
    },
    replyComment: function(e){
        console.log(e)
        this.setData({
            inputShowed: true,
            commentTarget: e.currentTarget.dataset.commentid,
            inputHolder: `回复 ${e.currentTarget.dataset.creatorname}`
        })
    },
    bindLike: function(e){
        var that = this
        if(that.data.dislikeIcon == icons.dislike1){
            wx.request({
                url: `http://${app.globalData.target}/post/dislikePost`,
                data: {
                    postId: that.data.postContent.postId
                },
                header: {
                    "hello-token": wx.getStorageSync("hello-token")
                },
                method: 'GET',
                success: function(result){
                    that.setData({
                        dislikeIcon: icons.dislike0
                    })
                    wx.request({
                        url: `http://${app.globalData.target}/post/likePost`,
                        data: {
                            postId: that.data.postContent.postId
                        },
                        header: {
                            "hello-token": wx.getStorageSync("hello-token")
                        },
                        method: 'GET',
                        success: function(result){
                            if(that.data.likeIcon == icons.like0){
                                that.setData({
                                    likeIcon: icons.like1
                                })
                            }else{
                                that.setData({
                                    likeIcon: icons.like0
                                })
                            }
                        },
                        fail: function(e){
                            console.log(e)
                        }
                    })
                },
                fail: function(e){
                    console.log(e)
                }
            })
        }else{
            wx.request({
                url: `http://${app.globalData.target}/post/likePost`,
                data: {
                    postId: that.data.postContent.postId
                },
                header: {
                    "hello-token": wx.getStorageSync("hello-token")
                },
                method: 'GET',
                success: function(result){
                    if(that.data.likeIcon == icons.like0){
                        that.setData({
                            likeIcon: icons.like1
                        })
                    }else{
                        that.setData({
                            likeIcon: icons.like0
                        })
                    }
                },
                fail: function(e){
                    console.log(e)
                }
            })
        }
    },
    bindDislike: function(e){
        var that = this
        if(that.data.likeIcon == icons.like1){
            wx.request({
                url: `http://${app.globalData.target}/post/likePost`,
                data: {
                    postId: that.data.postContent.postId
                },
                header: {
                    "hello-token": wx.getStorageSync("hello-token")
                },
                method: 'GET',
                success: function(result){
                    that.setData({
                        likeIcon: icons.like0
                    })
                    wx.request({
                        url: `http://${app.globalData.target}/post/dislikePost`,
                        data: {
                            postId: that.data.postContent.postId
                        },
                        header: {
                            "hello-token": wx.getStorageSync("hello-token")
                        },
                        method: 'GET',
                        success: function(result){
                            that.setData({
                                dislikeIcon: icons.dislike1
                            })
                        },
                        fail: function(e){
                            console.log(e)
                        }
                    })
                },
                fail: function(e){
                    console.log(e)
                }
            })
        }else{
            wx.request({
                url: `http://${app.globalData.target}/post/dislikePost`,
                data: {
                    postId: that.data.postContent.postId
                },
                header: {
                    "hello-token": wx.getStorageSync("hello-token")
                },
                method: 'GET',
                success: function(result){
                    if(that.data.dislikeIcon == icons.dislike0){
                        that.setData({
                            dislikeIcon: icons.dislike1
                        })
                    }else{
                        that.setData({
                            dislikeIcon: icons.dislike0
                        })
                    }
                },
                fail: function(e){
                    console.log(e)
                }
            })
        }
    },
    bindStar: function(e){
        var that = this
        wx.request({
            url: `http://${app.globalData.target}/post/collectPost`,
            data: {
                postId: that.data.postContent.postId
            },
            header: {
                "hello-token": wx.getStorageSync("hello-token")
            },
            method: 'GET',
            success: function(result){
                if(that.data.starIcon == icons.star0){
                    that.setData({
                        starIcon: icons.star1
                    })
                }else{
                    that.setData({
                        starIcon: icons.star0
                    })
                }
            },
            fail: function(e){
                console.log(e)
            }
        })
    },
    likeComment: function(e){
        var that = this
        var idx = e.currentTarget.dataset.idx
        var likeSum = that.data.commentList[idx].likeSum
        var dislikeSum = that.data.commentList[idx].dislikeSum
        if(that.data.commentList[idx].hasDislike){
            wx.request({
                url: `http://${app.globalData.target}/comment/dislike`,
                data: {
                    commentId: e.currentTarget.dataset.commentid
                },
                header: {
                    "hello-token": wx.getStorageSync("hello-token")
                },
                method: 'GET',
                success: function(result){
                    that.setData({
                        [`commentList[${idx}].hasDislike`]: false,
                        [`commentList[${idx}].dislikeSum`]: dislikeSum - 1
                    })
                    wx.request({
                        url: `http://${app.globalData.target}/comment/like`,
                        data: {
                            commentId: e.currentTarget.dataset.commentid
                        },
                        header: {
                            "hello-token": wx.getStorageSync("hello-token")
                        },
                        method: 'GET',
                        success: function(result){
                            //已知commentId，要改commentList中对应commentId相等的likeSum和hasLike
                            that.setData({
                                [`commentList[${idx}].hasLike`]: true,
                                [`commentList[${idx}].likeSum`]: likeSum + 1
                            })
                        },
                        fail: function(e){
                            console.log(e)
                        }
                    })
                },
                fail: function(e){
                    console.log(e)
                }
            })
        }else{
            wx.request({
                url: `http://${app.globalData.target}/comment/like`,
                data: {
                    commentId: e.currentTarget.dataset.commentid
                },
                header: {
                    "hello-token": wx.getStorageSync("hello-token")
                },
                method: 'GET',
                success: function(result){
                    //已知commentId，要改commentList中对应commentId相等的likeSum和hasLike
                    that.setData({
                        [`commentList[${idx}].hasLike`]: true,
                        [`commentList[${idx}].likeSum`]: likeSum + 1
                    })
                },
                fail: function(e){
                    console.log(e)
                }
            })
        }
        
    },
    cancelLikeComment: function(e){
        var that = this
        var idx = e.currentTarget.dataset.idx
        var likeSum = that.data.commentList[idx].likeSum
        wx.request({
            url: `http://${app.globalData.target}/comment/like`,
            data: {
                commentId: e.currentTarget.dataset.commentid
            },
            header: {
                "hello-token": wx.getStorageSync("hello-token")
            },
            method: 'GET',
            success: function(result){
                that.setData({
                    [`commentList[${idx}].hasLike`]: false,
                    [`commentList[${idx}].likeSum`]: likeSum - 1
                })
            },
            fail: function(e){
                console.log(e)
            }
        })
    },
    dislikeComment: function(e){
        var that = this
        var idx = e.currentTarget.dataset.idx
        var likeSum = that.data.commentList[idx].likeSum
        var dislikeSum = that.data.commentList[idx].dislikeSum
        if(that.data.commentList[idx].hasLike){
            wx.request({
                url: `http://${app.globalData.target}/comment/like`,
                data: {
                    commentId: e.currentTarget.dataset.commentid
                },
                header: {
                    "hello-token": wx.getStorageSync("hello-token")
                },
                method: 'GET',
                success: function(result){
                    that.setData({
                        [`commentList[${idx}].hasLike`]: false,
                        [`commentList[${idx}].likeSum`]: likeSum - 1
                    })
                    wx.request({
                        url: `http://${app.globalData.target}/comment/dislike`,
                        data: {
                            commentId: e.currentTarget.dataset.commentid
                        },
                        header: {
                            "hello-token": wx.getStorageSync("hello-token")
                        },
                        method: 'GET',
                        success: function(res){
                            that.setData({
                                [`commentList[${idx}].hasDislike`]: true,
                                [`commentList[${idx}].dislikeSum`]: dislikeSum + 1
                            })
                        },
                        fail: function(e){
                            console.log(e)
                        }
                    })
                },
                fail: function(e){
                    console.log(e)
                }
            })
        }else{
            wx.request({
                url: `http://${app.globalData.target}/comment/dislike`,
                data: {
                    commentId: e.currentTarget.dataset.commentid
                },
                header: {
                    "hello-token": wx.getStorageSync("hello-token")
                },
                method: 'GET',
                success: function(res){
                    that.setData({
                        [`commentList[${idx}].hasDislike`]: true,
                        [`commentList[${idx}].dislikeSum`]: dislikeSum + 1
                    })
                },
                fail: function(e){
                    console.log(e)
                }
            })
        }
        
    },
    cancelDislikeComment: function(e){
        var that = this
        var idx = e.currentTarget.dataset.idx
        var dislikeSum = that.data.commentList[idx].dislikeSum
        wx.request({
            url: `http://${app.globalData.target}/comment/dislike`,
            data: {
                commentId: e.currentTarget.dataset.commentid
            },
            header: {
                "hello-token": wx.getStorageSync("hello-token")
            },
            method: 'GET',
            success: function(result){
                that.setData({
                    [`commentList[${idx}].hasDislike`]: false,
                    [`commentList[${idx}].dislikeSum`]: dislikeSum - 1
                })
            },
            fail: function(e){
                console.log(e)
            }
        })
    }
})